//
//  YZCollectionVC.m
//  CollectionTest
//
//  Created by yuze on 2016/10/19.
//  Copyright © 2016年 yuze. All rights reserved.
//

#import "YZFlowCollectionVC.h"

#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBAColor(r, g, b ,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define RandColor RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

@interface YZFlowCollectionVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

static NSString *CollectionCellReuseId = @"collectionCelllIdTest";
static NSString *CollectionHeaderReuseId = @"collectionHeaderIdTest";
static NSString *CollectionFooterReuseId = @"collectionFooterIdTest";

@implementation YZFlowCollectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"流式布局";
    /// 创建布局样式 基类：UICollectionViewLayout 子类：UICollectionViewFlowLayout
    ///
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置它的滚动方向 UICollectionViewScrollDirectionHorizontal / UICollectionViewScrollDirectionVertical
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置item大小
    flowLayout.itemSize = CGSizeMake(66, 66);
    // 设置水平方向的空隙
    flowLayout.minimumInteritemSpacing = 6;
    // 设置垂直方向行之间的空隙
    flowLayout.minimumLineSpacing = 12;
    
    /// UICollectionView 实例化
    ///
    CGRect collectionViewRect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    
    /// 注册Cell类
    ///
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionCellReuseId];
    
    /// 注册Header
    ///
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:CollectionHeaderReuseId];
    
    /// 注册Footer
    ///
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:CollectionFooterReuseId];
    
    [collectionView reloadData];
}



#pragma mark -- UICollectionViewDelegateFlowLayout

/// 设置collectionView 上左下右边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(10, 20);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(10, 20);
}

//////////////////////
/// 可以使用  UICollectionViewFlowLayout .itemSize .minimumInteritemSpacing .minimumLineSpacing 来实现统一设置
/////////////////////

///// 设置item的大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(120, 120);
//}
//
///// 设置item横向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}
//
///// 设置item垂直方向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 50;
//}

#pragma mark -- UICollectionViewDataSource

/// 分为几部分
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

/// 多少个单元格
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 66;
}

/// 获取注册cell item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseId forIndexPath:indexPath];
    
    cell.contentView.backgroundColor = RandColor;
    return cell;
}

/// 返回 标题头和标题尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:CollectionHeaderReuseId forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor purpleColor];
        return headerView;
        
    } else {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:CollectionFooterReuseId forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor yellowColor];
        return footerView;
    }
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell= [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = RandColor;
}


@end
